package com.network.controller;


import com.network.client.ClientService;
import com.network.pojo.Result;
import com.network.pojo.User;
import com.network.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private ClientService clientService;

    @PostMapping("/login")
    public Result loginToServer(@RequestBody User user) throws IOException {

        Result result= loginService.loginToServer(user);
        if (result.getCode().equals(1)) {

            new Thread(()->{
                try {
                    clientService.startClient(user.getUsername());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();


            return Result.success();
        }

        return Result.error("用户名或密码错误");

    }

}
